package com.qingxin.sys.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

/**
 * 系统异常处理类，转到相应页面或提示相应错误信息
 */
public class QxMappingExceptionResolver extends SimpleMappingExceptionResolver {
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		if(ex!=null){
			System.out.println("异常请求："+request.getServletPath());
			ex.printStackTrace();
		}
		
		String requestURI = request.getRequestURI();
		if(requestURI.endsWith(".do")){	//普通请求
			String viewName = determineViewName(ex, request);
			if (viewName != null) {
				Integer statusCode = determineStatusCode(request, viewName);
				if (statusCode != null) {
					applyStatusCodeIfPossible(request, response, statusCode);
				}
				String path = request.getContextPath();
				request.setAttribute("path", path);
				return getModelAndView(viewName, ex, request);
			}
		}
		else if(requestURI.endsWith(".json")){	//ajax请求
			return getModelAndView("jsonView", ex, request);
		}
		else if(requestURI.endsWith(".xml")){	//xml请求
			//TODO:添加xml异常处理方法
		}
			
		return null;
	}
}
